/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package meteocal.gui;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.security.Principal;
import java.util.Collection;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import meteocal.business.boundary.EventDeleteFacade;
import meteocal.business.boundary.EventFacade;
import meteocal.business.boundary.NotificationFacade;
import meteocal.business.entity.Event;
import meteocal.business.security.entity.User;

/**
 *
 * @author Francesca
 */
@Named(value = "deleteEventBean")
@RequestScoped
public class DeleteEventBean implements Serializable {

    private Integer idEvent;
    
    @Inject
    ShowCalendarBean showCalendarBean;
    
    @Inject
    Principal principal;
    
    @EJB
    EventFacade ef;
    
    @EJB
    NotificationFacade nf;
    
    @EJB
    EventDeleteFacade edf;
    /**
     * Creates a new instance of DeleteEventBean
     */
    public DeleteEventBean() {
    }

    public Integer getIdEvent() {
        return idEvent;
    }

    public void setIdEvent(Integer idEvent) {
        this.idEvent = idEvent;
    }
 
    public EventFacade getEf() {
        return ef;
    }

    public void setEf(EventFacade ef) {
        this.ef = ef;
    }
    
    public void generateMessage(String message) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!",message) );
    }
    
    /**
     * Before deleting an event a notification must be send to
     * users invited to that event.
     * In this case the type of the notification is 2
     * @return 
     */
    public String deleteEvent(){
        
        
        
        Collection<User> invited;
        Event event;
        event = ef.eventDetails(idEvent);
        
        //Only the creator can modify
        if(!event.getCreator().getUsername().equals(principal.getName())){
            generateMessage("You are not the creator of this event!");
                return null;
        }
        
        invited = event.getParticipants();
        edf.create(event.getIdEvent(),event.getName(), event.getStarting_date(),event.getStartingTime(),event.getEnding_date(), event.getEndingTime(), event.getPlace(), event.getCreator().getUsername());
        for(User u: invited){
           nf.create(event, u.getUsername(), 2);
        }
        
        ef.removeEvent(idEvent);
        return showCalendarBean.deleteEvent(event);    
    }
}
